package com.rz.paas.test.bottomView

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.orhanobut.hawk.Hawk
import com.rz.paas.test.R
import com.rz.paas.test.SettingDetail.SettingDetailAdapter
import com.rz.paas.test.SettingDetail.StickHeaderDirection
import com.rz.paas.test.SettingDetail.TypeData
import com.rz.paas.test.constant.AppConstant
import com.rz.paas.test.constant.OptionsData
import com.rz.paas.test.inChannel.viewModel.CommunicationViewModel
import kotlinx.android.synthetic.main.activity_setting_detail.item_detail
import kotlinx.android.synthetic.main.activity_setting_detail.type_title
import kotlinx.android.synthetic.main.channel_card_fragment.*

class InChannelSettingDetailFragment : BottomSheetDialogFragment() {
    val communicationViewModel by activityViewModels<CommunicationViewModel>()

    companion object {
        const val OPTION_ID = "OPTION_ID"
        const val TYPE_ITEM_SELECT = "TYPE_ITEM_SELECT"

        fun newInstance(optionId: Int, selected: Int): InChannelSettingDetailFragment {
            val fragment = InChannelSettingDetailFragment()
            val bundle = Bundle()
            bundle.putInt(InChannelSettingDetailFragment.OPTION_ID, optionId)
            bundle.putInt(InChannelSettingDetailFragment.TYPE_ITEM_SELECT, selected)
            fragment.setArguments(bundle)
            return fragment
        }

    }

    private val adapter by lazy {
        SettingDetailAdapter(requireContext())
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.channel_card_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var optionId = arguments?.getInt(OPTION_ID)!!
        var selectNum = arguments?.getInt(TYPE_ITEM_SELECT)!!


        val optionsData = AppConstant.dataMap[optionId]
        title.text = optionsData.subpage
        type_title.text = optionsData.title
        initRecyclerView(optionsData, selectNum)

        itemSelect(optionsData)

        completed.setOnClickListener {
            if (!TextUtils.isEmpty(globalGptionsData?.name)){
                Hawk.put(globalGptionsData?.name, selectedIndex)
                communicationViewModel.optionIndex(optionId,selectedIndex)
            }

            dismiss()
        }

        cancel.setOnClickListener {
            dismiss()
        }

    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState)
    }

    private fun initRecyclerView(optionsData: OptionsData, selectNum: Int) {
        item_detail.layoutManager = LinearLayoutManager(requireContext())
        item_detail.adapter = adapter
        if (optionsData.resId == R.array.encode_resolution_data) {
            item_detail.addItemDecoration(StickHeaderDirection(requireContext(), true))
        }

        val stringArray = resources.getStringArray(optionsData.resId)
        val listData = mutableListOf<TypeData>()
        initData(stringArray, selectNum, listData)
        adapter.data = listData
    }


    var selectedIndex = 0
    var globalGptionsData: OptionsData? = null
    private fun itemSelect(optionsData: OptionsData) {
        adapter.setItemClickListener { data, position ->
            val oldSelected = adapter.data[selectedIndex]
            oldSelected.isSelected = false
            adapter.setData(selectedIndex, oldSelected)

            val newSelected = adapter.data[position]
            newSelected.isSelected = true
            selectedIndex = position
            adapter.setData(position, newSelected)
            globalGptionsData = optionsData
        }
    }

    private fun initData(
        stringArray: Array<out String>, selectNum: Int, listData: MutableList<TypeData>
    ) {
        for (index in stringArray.indices) {
            val typeData = TypeData()
            typeData.name = stringArray[index]
            if (typeData.name.contains("*")) {
                typeData.groupName = typeData.name.split("*")[1]
                if (typeData.groupName.toInt() >= 720) {
                    typeData.groupName = "其他"
                }
            }
            if (selectNum == index) {
                typeData.isSelected = true
                selectedIndex = index
            } else {
                typeData.isSelected = false
            }

            listData.add(typeData)
        }
    }

    override fun onStart() {
        super.onStart()
        var dialog = dialog as BottomSheetDialog
        var bottomSheet = dialog.window?.findViewById<FrameLayout>(R.id.design_bottom_sheet)
        if (bottomSheet != null) {
            bottomSheet.background = ColorDrawable(Color.TRANSPARENT)
            var layoutParams = bottomSheet.layoutParams as CoordinatorLayout.LayoutParams
            layoutParams.height = getPeekHeight()
            bottomSheet.layoutParams = layoutParams

            var from = BottomSheetBehavior.from(bottomSheet)
            from.state = BottomSheetBehavior.STATE_EXPANDED
        }

    }

    private fun getPeekHeight(): Int {
        var screenHeight = resources.displayMetrics.heightPixels
        return screenHeight - screenHeight / 6 - 30
    }
}